<?php

/**
 *   OPENSHOP AJAX 翻页类
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: Page.class.php,v 1.0 2009/06/17 08:36:58 created $
 *   @author      FillBag <fillbag@hotmail.com>
 */

class Page{
	var $psize 				  =  0;			//每页显示记录数
    var $tpage                =  0;			//总页数
	var $pers                 =  0;			// 记录组总数
	var $pernum               =  2;			//页码偏移量	
    var $page                 =  null;		//当前页码
    var $limit                =  '';		//MySQL分页生成语句
	var $page_stmt; 			   			//分页sql语句
	var $page_count;               			//分页sql记录总数查询语句或传入数字
	
	//初始化，传入一个sql数值
	function __construct($sql,$sql_count,$psize=5){
		$this->Page($sql,$sql_count,$psize);
	}
	
	function Page($sql,$sql_count,$psize=5){
		$this->page_stmt = $sql;
		$this->page_count = $sql_count;
		$this->psize = $psize;
	}
	
	function get(){
		$this->page   = $this->page ? $this->page :1;
		$this->limit  = ($this->page -1)*$this->psize.','.$this->psize; // 用于 MySQL 分页生成语句 //  
	}
	
    function total($number)
    {   //统计页码数
        $this->tpage = ceil($number / $this->psize);
        $this->pers  = ceil($this->tpage / $this->pernum);
    }
    
    function jumpBar()
    {
    	$number = $this->page_count;
        $this->get();
        $this->total($number);
        $setpage   = $this->page ? ceil($this->page/$this->pernum) : 1;      
        $pagenum   = ($this->tpage > $this->pernum) ? $this->pernum : $this->tpage;
        if ($number  <= $this->psize) {
             $text = '<div id="bbsBottomTableTd1P"><div class="pagenav">查看共'.$number.' 个商品</div></div>';
        } else {
            $text = '<div id="bbsBottomTableTd1P"><div class="pagenav">查看共'.$number.' 个商品&nbsp;&nbsp;&nbsp;';
            if ($this->page > 1) {
                $text .= '<a title="第一页" href="javascript:;" class="pagenav_index" onclick="xajax_goto_page(1, xajax.getFormValues(\'controlsForm\'));return false;">First</a>...';
            }
            if ($setpage > 1) {
                $lastsetid = ($setpage-1)*$this->pernum;
                $text .= '<a title="上一列" href="javascript:;" onclick="xajax_goto_page(\''.$lastsetid.'\', xajax.getFormValues(\'controlsForm\'));return false;"><<</a>';
            }
            $i = ($setpage-1)*$this->pernum;
            for($j=$i; $j<($i+$pagenum) && $j<$this->tpage; $j++) {
                $newpage = $j+1;
                if ($this->page == $j+1) {
                    $text .= '<span>'.($j+1).'</span>';
                } else {
                    $text .= '<a title="第'.($j+1).'页" href="javascript:;"
                              onclick="xajax_goto_page(\''.($j+1).'\', xajax.getFormValues(\'controlsForm\'));return false;">'.($j+1)."</a>";
                }
            }  
            if ($setpage < $this->pers) {
                $nextpre = $setpage*($this->pernum+1);
                $text .= '<a title="下一列" href="javascript:;"
                           onclick="xajax_goto_page(\''.$nextpre.'\', xajax.getFormValues(\'controlsForm\'));return false;">>></a>';
            }
            if ($this->page < $this->tpage){
                $next = $this->page+1;
                $text .= '...<a title="最后一页" href="javascript:;" 
                          onclick="xajax_goto_page(\''.$this->tpage.'\', xajax.getFormValues(\'controlsForm\'));return false;">Last</a>';
            }       
            $text .= "</div></div>";
        }
        return $text;   	
    }
    
	function getRecordCounts(){
		global $db,$sqlNum;
		if(gettype($this->page_count)!=integer){
			$sqlNum++;
            $tmp = $db->getOne($this->page_count);
                                   
			if(isset($tmp) && $tmp>0){
				$this->page_count=$tmp; //返回所查询得到的个数
			}else{
				$this->page_count= 0;
			}
		}	
		return $this->page_count;
	}
	
	function setData(){
		global $db,$sqlNum;
		$sqlNum++;
		$result = $db->query("set names utf8");
		$result = $db->query($this->page_stmt." LIMIT ".$this->limit);
		$temp = array();
		while($row = $db->fetchRow($result)){
 			array_push($temp,$row);
		}
		return $temp;
	}
	
	function getData(){
		return array(
					'recordCounts'=>$this->getRecordCounts(),
					'jumpBar'=>$this->jumpBar(),
                    'list'=>$this->setData()
					 );
	}
}
?>